import axios from "axios";
import { getTokenName, messageConfirm, messageTip, removeToken } from "../util/util.js";
// 创建axios实例
const axiosInstance1 = axios.create({
    baseURL: "http://localhost:8080", // 设置基础URL
});
// GET请求
export function doGet(url, params) {
    return axiosInstance1({
        method: "get",
        url: url,
        params: params,
    });
}
// POST请求
export function doPost(url, data) {
    return axiosInstance1({
        method: "post",
        url: url,
        data: data,
    });
}
// PUT请求
export function doPut(url, data) {
    return axiosInstance1({
        method: "put",
        url: url,
        data: data,
    });
}
// DELETE请求
export function doDelete(url, params) {
    return axiosInstance1({
        method: "delete",
        url: url,
        params: params,
    });
}
// 请求拦截器 - 添加token
axiosInstance1.interceptors.request.use((config) => {
    let token = window.sessionStorage.getItem(getTokenName()) || window.localStorage.getItem(getTokenName());
    if (token) {
        config.headers['Authorization'] = token;
    }
    return config;
}, (error) => {
    return Promise.reject(error);
});
// 响应拦截器 - 处理登录失效
axiosInstance1.interceptors.response.use((response) => {
    if (response.data.code > 900) {
        messageConfirm(response.data.msg + "，是否重新去登录？").then(() => {
            removeToken();
            window.location.href = "/";
        }).catch(() => {
            messageTip("取消去登录", "warning");
        });
        return;
    }
    return response;
}, function (error) {
    return Promise.reject(error);
});
